﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

namespace Pg.BioMedics.ASR.Client
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        #region Event handlers

        private void MainForm_Load(object sender, EventArgs e)
        {
            // register SSL certification validation callback
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);

            // show login form
            if (new LoginForm().ShowDialog(this) == DialogResult.Cancel)
            {
                Close();
            }
        }

        private void RefreshButton_Click(object sender, EventArgs e)
        {
            try
            {
                using (WebServices.ASRServiceClient client = new WebServices.ASRServiceClient())
                {
                    client.ClientCredentials.UserName.UserName = ClientCredentialsCache.GetInstance().Credential.UserName;
                    client.ClientCredentials.UserName.Password = ClientCredentialsCache.GetInstance().Credential.Password;

                    resultingTasksBindingSource.DataSource = client.GetResultingTasks();
                    pendingTasksBindingSource.DataSource = client.GetPendingTasks();
                    waitingTasksBindingSource.DataSource = client.GetWaitingTasks();
                }

                refreshTimer.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void NewTaskButton_Click(object sender, EventArgs e)
        {
            new NewTaskForm().ShowDialog(this);
        }

        private void resultingTasksDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            WebServices.Result[] results = null;
            using (WebServices.ASRServiceClient client = new WebServices.ASRServiceClient())
            {
                client.ClientCredentials.UserName.UserName = ClientCredentialsCache.GetInstance().Credential.UserName;
                client.ClientCredentials.UserName.Password = ClientCredentialsCache.GetInstance().Credential.Password;

                WebServices.Task task = (WebServices.Task)resultingTasksBindingSource.Current;
                results = client.GetResults(task);
            }

            new TaskResultsForm().ShowDialog(this, results);
        }

        private void resultingTasksDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
 
        }

        private void refreshTimer_Tick(object sender, EventArgs e)
        {
            refreshTimer.Enabled = false;
            refreshButton.PerformClick();
        }

        private void finishToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                using (WebServices.ASRServiceClient client = new WebServices.ASRServiceClient())
                {
                    client.ClientCredentials.UserName.UserName = ClientCredentialsCache.GetInstance().Credential.UserName;
                    client.ClientCredentials.UserName.Password = ClientCredentialsCache.GetInstance().Credential.Password;

                    WebServices.Task task = (WebServices.Task)resultingTasksBindingSource.Current;
                    client.FinishTask(task);
                }

                refreshButton.PerformClick();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void showResultsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WebServices.Result[] results = null;
            using (WebServices.ASRServiceClient client = new WebServices.ASRServiceClient())
            {
                client.ClientCredentials.UserName.UserName = ClientCredentialsCache.GetInstance().Credential.UserName;
                client.ClientCredentials.UserName.Password = ClientCredentialsCache.GetInstance().Credential.Password;

                WebServices.Task task = (WebServices.Task)resultingTasksBindingSource.Current;
                results = client.GetResults(task);
            }

            new TaskResultsForm().ShowDialog(this, results);
        }

        private void resultingTasksContextMenuStrip_Opening(object sender, CancelEventArgs e)
        {
            e.Cancel = (resultingTasksBindingSource.Current == null);
        }

        #endregion                                                                      

        #region Support methods

        public static bool ValidateServerCertificate(object sender, 
                                                     X509Certificate certificate,
                                                     X509Chain chain,
                                                     SslPolicyErrors sslPolicyErrors)
        {
            return true;
        }

        #endregion
    }
}
